package com.teamall.framework.util;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.*;

public class UploadFileUtil {
    public static List<Map> saveFiles(HttpServletRequest request) {
        List<Map> resultList = new ArrayList<Map>();
        try {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            multipartRequest.setCharacterEncoding("UTF-8");
            String dataStr = DateUtils.date2StringDay(new Date());
            String uploadbasepath = MessageResourceUtils.getMessage("filePath") + dataStr + "\\";
            File file = new File(uploadbasepath);
            if (!file.exists()) {
                file.mkdirs();
            }
            Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
            for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
                String imgId = IdCreateUtils.createId();
                MultipartFile mf = entity.getValue();
                byte[] bytes = mf.getBytes();
                String fileName = mf.getOriginalFilename();
                String endName = fileName.substring(fileName.lastIndexOf(".") + 1);
                String realPath = uploadbasepath + "/" + imgId + "." + endName;
                BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(realPath)));
                stream.write(bytes);
                stream.close();
                Map resultMap = new HashMap();
                resultMap.put("imgId", imgId);
                String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
                url = url + "/app/file/getFile?imgId=" + imgId;
                resultMap.put("imgUrl", url);
                resultMap.put("realPath", realPath);
                resultList.add(resultMap);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultList;
    }
    public static List<Map> saveFiles1(HttpServletRequest request) {
        List<Map> resultList = new ArrayList<Map>();
        try {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            multipartRequest.setCharacterEncoding("UTF-8");
            String dataStr = DateUtils.date2StringDay(new Date());
            String uploadbasepath = MessageResourceUtils.getMessage("filePath") + dataStr + "\\";
            File file = new File(uploadbasepath);
            if (!file.exists()) {
                file.mkdirs();
            }
            Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
            for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
                String imgId = IdCreateUtils.createId();
                MultipartFile mf = entity.getValue();
                if(mf.getSize()==0)
                    break;
                byte[] bytes = mf.getBytes();
                String fileName = mf.getOriginalFilename();
                String endName = fileName.substring(fileName.lastIndexOf(".") + 1);
                String realPath = uploadbasepath + "/" + imgId + "." + endName;
                BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(realPath)));
                stream.write(bytes);
                stream.close();
                Map resultMap = new HashMap();
                resultMap.put("imgId", imgId);
                String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
                url = url + "/app/file/getReportFile?imgId=" + imgId;
                resultMap.put("imgUrl", url);
                resultMap.put("realPath", realPath);
                resultMap.put("fileName", fileName);
                resultList.add(resultMap);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultList;
    }

    public static Map uploadFiles(HttpServletRequest request) {
        Map resultMap = new HashMap();
        try {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            multipartRequest.setCharacterEncoding("UTF-8");
            String dataStr = DateUtils.date2StringDay(new Date());
            String uploadbasepath = MessageResourceUtils.getMessage("filePath") + dataStr + "\\";
            File file = new File(uploadbasepath);
            if (!file.exists()) {
                file.mkdirs();
            }
            Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
            for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
                String imgId = IdCreateUtils.createId();
                MultipartFile mf = entity.getValue();
                byte[] bytes = mf.getBytes();
                String fileName = mf.getOriginalFilename();
                String endName = fileName.substring(fileName.lastIndexOf(".") + 1);
                String realPath = uploadbasepath + "/" + imgId + "." + endName;
                BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(realPath)));
                stream.write(bytes);
                stream.close();
                String url = "/" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
                url = url + "/app/file/getFile?imgId=" + imgId;
                resultMap.put("url", url);
                resultMap.put("type", "."+endName);
                resultMap.put("title",imgId + "." + endName);
                resultMap.put("state","SUCCESS");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultMap;
    }
}